//app.js
var code = "";
App({
     onLaunch: function () {
          // 展示本地存储能力
          var logs = wx.getStorageSync('logs') || []
          logs.unshift(Date.now())
          wx.setStorageSync('logs', logs)
          // 登录
          wx.login({
               success: res => {
                    code = res.code;
                    this.login(code)
                    // 发送 res.code 到后台换取 openId, sessionKey, unionId
               }
          })
     },
     login: function (code) {
          let that = this

          // 获取用户信息
          wx.getSetting({

               success: res => {
                    wx.request({
                         url: that.globalData.baseURL+'/member/loginWithCode',

                         method: 'post',
                         header: {
                              "Content-Type": "application/x-www-form-urlencoded"
                         },
                         data: {
                              'code': code
                         },

                         success: function (result) {

                              that.globalData.userInfo = result.data.result

                              if (that.userInfoReadyCallback) {
                                   that.userInfoReadyCallback(result.data.result)
                              }
                         }
                    })
               }
          })
     },
     globalData: {
          userInfo: null,
           baseURL: 'https://homekuaishan.jnby.com/sunday/small/'
         //baseURL: 'http://jnby.vipgz1.idcfengye.com/sunday/small/'
     },
     synPostData: function (url, data) {
          var that = this;
          return new Promise(function (resolve, reject) {
               wx.request({
                    url: that.globalData.baseURL + url,
                    data: data,
                    method: "POST",
                    header: {
                         "Content-Type": "application/x-www-form-urlencoded"
                    },
                    success: function (res) {
                         resolve(res);
                    },
                    fail: function (res) {
                         reject(res);
                    },
               })


          });
     }
})



